package com.td.module.system.controller.admin.video;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.system.controller.admin.video.vo.list.*;
import com.td.module.system.convert.video.ListConvert;
import com.td.module.system.dal.dataobject.video.ListDO;
import com.td.module.system.service.video.ListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Api(tags = "管理后台 - 摄像头列")
@RestController
@RequestMapping("/video/list")
@Validated
public class ListController {

    @Resource
    private ListService listService;

    @PostMapping("/create")
    @ApiOperation("创建摄像头列")
    public CommonResult<String> createList(@Valid @RequestBody ListCreateReqVO createReqVO) {
        return success(listService.createList(createReqVO));
    }

    @PostMapping("/creates")
    @ApiOperation("创建摄像头列")
    public CommonResult<String> createLists(@Valid @RequestBody List<ListCreateReqVO> list) {
        return success(listService.createLists(list));
    }

    @PutMapping("/update")
    @ApiOperation("更新摄像头列")
    public CommonResult<Boolean> updateList(@Valid @RequestBody ListUpdateReqVO updateReqVO) {
        listService.updateList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除摄像头列")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    public CommonResult<Boolean> deleteList(@RequestParam("id") String id) {
        listService.deleteList(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得摄像头列")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<ListRespVO> getList(@RequestParam("id") String id) {
        ListDO list = listService.getList(id);
        return success(ListConvert.INSTANCE.convert(list));
    }

    @GetMapping("/list")
    @ApiOperation("获得摄像头列列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<ListRespVO>> getLists(@RequestParam("ids") Collection<String> ids) {
        List<ListDO> list = listService.getListList(ids);
        return success(ListConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得摄像头列分页")
    public CommonResult<PageResult<ListRespVO>> getListPage(@Valid ListPageReqVO pageVO) {
        PageResult<ListDO> pageResult = listService.getListPage(pageVO);
        return success(ListConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出摄像头列 Excel")
    @OperateLog(type = EXPORT)
    public void exportListExcel(@Valid ListExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ListDO> list = listService.getListList(exportReqVO);
        // 导出 Excel
        List<ListExcelVO> datas = ListConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "摄像头列.xls", "数据", ListExcelVO.class, datas);
    }

}
